package routers

import (
	v1 "ginblog/api/v1"
	v2 "ginblog/api/v2"
	"ginblog/middeware"
	"ginblog/utils"
	"github.com/gin-gonic/gin"
)

func InitRouter() {
	gin.SetMode(utils.AppMode)
	//r := gin.Default()               // 默认添加2个中间件
	r := gin.New()
	r.Use(gin.Recovery())
	r.Use(middeware.Logger())
	r.Use(middeware.Cors())
	r.LoadHTMLGlob("templates/**/*") // 加载模板文件
	r.Static("/static", "static")    // 加载静态资源
	auth := r.Group("api/v1")
	auth.Use(middeware.JwtToken())
	{
		// 用户模块的路由接口
		// 查询用户是否存在
		auth.PUT("user/:id", v1.EditUser)
		auth.DELETE("user/:id", v1.DeleteUser)
		// 分类模块的路由接口
		auth.POST("category/add", v1.AddCategory)

		auth.PUT("category/:id", v1.EditCategory)
		auth.DELETE("category/:id", v1.DeleteCategory)

		// 文章模块的路由接口
		auth.POST("article/add", v1.AddArticle)

		auth.PUT("article/:id", v1.EditArticle)
		auth.DELETE("article/:id", v1.DeleteArticle)
		// 上传文件接口
		auth.POST("upload", v1.Upload)

	}
	routerv1 := r.Group("api/v1")
	{
		// 新增用户
		routerv1.POST("user/add", v1.AddUser)
		routerv1.GET("login", v1.Login)
		routerv1.POST("login", v1.Login)
		routerv1.GET("home", v1.Home)
		routerv1.GET("users", v1.GetUserList)
		routerv1.GET("categorys", v1.GetCategoryList)
		routerv1.GET("articles", v1.GetArticleList)
		routerv1.GET("article/cate/:id", v1.GetCateArticle)
		routerv1.GET("article/:id", v1.GetArticleInfo)
	}

	routerv2 := r.Group("api/v2")
	{
		// 用户模块的路由接口
		routerv2.GET("login", v2.Login)
		routerv2.POST("login", v2.Login)
		routerv2.GET("home", v2.Home)
	}
	r.Run(utils.HttpPort)
}
